package com.iqiyi.pps.epg.core.service.remind;

import com.google.common.collect.Lists;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.remind.MailRemindDao;
import com.iqiyi.pps.epg.core.model.remind.MailRemind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by karkarrotxu on 2015/9/1.
 */
@Service
@Transactional
public class MailRemindServiceImpl implements  MailRemindService{
    protected Logger logger = LoggerFactory.getLogger(MailRemindServiceImpl.class);

    @Autowired
    private MailRemindDao mailRemindDao;

    //public List<>

    @Override
    public MailRemind getMailRemind(String type, String entityId) {
        List<PropertyFilter> filters = Lists.newArrayList();
        filters.add(new PropertyFilter("EQS_type", type));
        filters.add(new PropertyFilter("EQS_entityId", entityId));
        List<MailRemind> mails = mailRemindDao.find(filters);
        if (mails.size() > 0) {
            return mails.get(0);
        } else {
            MailRemind newMail = new MailRemind();
            newMail.setType(type);
            newMail.setEntityId(entityId);
            return newMail;
        }
    }
    @Override
    public void save(MailRemind mailRemind){
        mailRemindDao.save(mailRemind);
    }

    public void saveMailHistory(MailRemind newMail, String title, String tos, String contents, Timestamp noRemind) {
        newMail.setTitle(title);
        newMail.setContent(contents);
        newMail.setTos(tos);
        if (noRemind != null) {
            newMail.setEndTime(noRemind);
        }
        try {
            save(newMail);
        } catch (Exception e) {
            logger.error("save MailRemind error,id:"+newMail.getId() , e);
        }
    }
}
